package com.gsyt.noti2win;

import android.app.Notification;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Icon;
import android.os.Bundle;
import android.os.Parcelable;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Base64;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

public class NotificationService extends NotificationListenerService {

    private static final String WECHAT_PACKAGE_NAME = "com.tencent.mm";

    private static final String MOBILEQQ_PACKAGE_NAME = "com.tencent.mobileqq";

    private static final String DINGTALK_PACKAGE_NAME = "com.alibaba.android.rimet";

    private ServiceAddr serviceAddr = ServiceAddr.getInstance();
    private AndroidExecutors executors = AndroidExecutors.getInstance();

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        super.onNotificationPosted(sbn);
        //通知来源包名
        String packageName = sbn.getPackageName();
        String notificationTitle = sbn.getNotification().extras.getString("android.title");
        String notificationText = sbn.getNotification().extras.getString("android.text");
        Icon largeIcon = sbn.getNotification().getLargeIcon();
        String icon = "";
        if (largeIcon != null) {
            Drawable drawable = largeIcon.loadDrawable(this);
            Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
            icon = bitmapToBase64(bitmap);
            Log.d("Avatar", "getLargeIcon");
        } else {
            Bundle extras = sbn.getNotification().extras;
            Parcelable parcelable = extras.getParcelable(Notification.EXTRA_LARGE_ICON);
            if (parcelable instanceof Bitmap) {
                Bitmap bitmap = (Bitmap) parcelable;
                icon = bitmapToBase64(bitmap);
                Log.d("Avatar", "EXTRA_LARGE_ICON");
            } else {
                Log.d("Avatar", "头像为空");
            }

        }
        Message message = null;
        int msgType;
        if (WECHAT_PACKAGE_NAME.equals(packageName)) {
            msgType=0;
        } else if (MOBILEQQ_PACKAGE_NAME.equals(packageName)) {
            msgType=1;
        } else if (DINGTALK_PACKAGE_NAME.equals(packageName)) {
            msgType=3;
            icon=null; //钉钉的头像获取的是图标 没意义
        }else
        {
            return;
        }
        if (!icon.equals(""))
            message = new Message(msgType, notificationTitle, notificationText, icon);
        else
            message = new Message(msgType, notificationTitle, notificationText);

        Gson gson = new Gson();
        String msgJson = gson.toJson(message);
        String ipAddress = serviceAddr.getIpAddress();
        if (ipAddress == null) {
            Toast.makeText(this, "请先设置服务器地址", Toast.LENGTH_LONG).show();
            return;
        }
        executors.executeJob(() -> {
            Utils.sendMsg(ipAddress, msgJson);
        });
        Log.d("TAGAAA", "onNotificationPosted->" + notificationTitle);
    }

    private String bitmapToBase64(Bitmap bitmap) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);  // 也可以用 JPEG
        byte[] byteArray = outputStream.toByteArray();
        return Base64.encodeToString(byteArray, Base64.NO_WRAP);
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        super.onNotificationRemoved(sbn);
    }

    @Override
    public StatusBarNotification[] getActiveNotifications() {
        return super.getActiveNotifications();
    }

    /**
     * 获取微信头像
     */
    private Bitmap getWeChatAvatar(Notification notification, Bundle extras) {
        // 可能的存储位置
        Bitmap avatar = null;
//        if (avatar == null) {
//            avatar = extras.getParcelable(Notification.EXTRA_SMALL_ICON);
//        }

        // 另一种方式：获取 RemoteViews 中的图片（复杂）
        if (avatar == null) {
            avatar = extractIconFromRemoteViews(notification);
        }
        return avatar;
    }

    /**
     * 解析 RemoteViews 获取头像（可能无效，微信 UI 可能变更）
     */
    private Bitmap extractIconFromRemoteViews(Notification notification) {
        try {
            RemoteViews views = notification.contentView;
            if (views == null) return null;

            Field field = views.getClass().getDeclaredField("mActions");
            field.setAccessible(true);
            List<Object> actions = (List<Object>) field.get(views);

            for (Object action : actions) {
                Field typeField = action.getClass().getDeclaredField("viewId");
                typeField.setAccessible(true);
                int viewId = typeField.getInt(action);

                Field valueField = action.getClass().getDeclaredField("value");
                valueField.setAccessible(true);
                Object value = valueField.get(action);

                if (value instanceof Bitmap) {
                    return (Bitmap) value;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
